package com.jbit.controller;

import com.jbit.dto.DmUser;
import com.jbit.utils.DmToken;
import com.jbit.utils.JwtUtils;
import com.jbit.utils.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user/api/v")
public class DmUserController {
    @PostMapping("/loadCurrentUserByToken")
    public ResponseEntity loadCurrentUserByToken(@RequestHeader String token) {
        DmUser user = JwtUtils.parseToken(token);
        if (user == null) {
            return ResponseEntity.sendUnLogin();
        } else {
            return ResponseEntity.sendSuccess(user);
        }
    }

    @PostMapping("/replaceToken")
    public ResponseEntity relpaceToken(@RequestBody DmToken token) {
        //替换token
        DmUser user = JwtUtils.parseToken(token.getToken());
        if (user == null) {
            return ResponseEntity.sendError("0001");
        } else {
            return ResponseEntity.sendSuccess(DmToken.getToken(user));
        }
    }
}
